package com.medata.service.impl;

import com.medata.dataObject.NewsInfo;
import com.medata.repository.NewsInfoRepository;
import com.medata.service.NewsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/12/30 0030 21:10
 * @description：
 */
@Service
public class NewsInfoServiceImpl implements NewsInfoService {
    @Autowired
    private NewsInfoRepository repository;

    @Override
    public void saveOrUpdate(NewsInfo newsInfo) {
        repository.save(newsInfo);
    }

    @Override
    public List<NewsInfo> find(Integer page, Integer size) {
        Sort sort=new Sort(Sort.Direction.DESC,"time");
        Pageable pageable=new PageRequest(page-1,size,sort);
        Page<NewsInfo> all = repository.findAll(pageable);
        return all.getContent();
    }

    @Override
    @Transactional
    public void delete(NewsInfo newsInfo) {
        repository.delete(newsInfo);
    }

    @Override
    public NewsInfo findByNewId(Integer id) {
        return repository.findByNewId(id);
    }

    @Override
    @Transactional
    public void deleteByNewId(Integer id) {
        repository.deleteByNewId(id);
    }
}
